import { getStorage } from "../../utils/storage"
Page({
  data: {
    semesterData: [],//学期数据
    semesterSelected: '', //默认选中的学期,
    actTypes: [],//活动类型
    actTypeScores: [],//活动类型积分统计
    signedActs: [],//已报名
    handingActs: [],//进行中
    finishedActs: [],//已结束
  },
  async onLoad(options) {
    let [actTypeRes, yearRes] = await wx.$http.all(
      wx.$http.post("/quality/actType/getDefaultActTypes"),
      wx.$http.post("/quality/actType/getActTypesByHalfYear")
    ).catch();

    let { statusCode, data: { code, data: yearData } } = yearRes;
    if (statusCode === 200 && code === 200) {
      let years = yearData.map(x => { return { text: x.halfYearDesc, value: x.halfYear } });
      this.setData({ semesterData: years });
      if (years.length > 0)
        this.setData({ semesterSelected: years[0].value })
    }
    if (actTypeRes.statusCode === 200 && actTypeRes.data.code === 200)
      this.setData({ actTypes: actTypeRes.data.data });
    this.getStudentData();
  },
  changeSemester(e) {
    this.setData({ semesterSelected: e.detail });
    this.getStudentData();
  },
  async getStudentData() {
    if (!this.data.semesterSelected)
      return;
    let query = { userId: getStorage('userId'), halfYear: this.data.semesterSelected };
    let [scores, signedActsRes, handingActsRes, finishedActsRes] =
      await wx.$http.all(
        wx.$http.post("/quality/student/getActivityScoreForStu", query),
        wx.$http.post("/quality/student/getStudentApplyedActLists", { ...query, actStatus: "3" }),//已报名
        wx.$http.post("/quality/student/getStudentApplyedActLists", { ...query, actStatus: "4" }),//进行中
        wx.$http.post("/quality/student/getStudentApplyedActLists", { ...query, actStatus: "5" }),//已结束
      );
    if (scores.statusCode === 200 && scores.data.code === 200) {
      scores.data.data.forEach(x => {
        x.percentage = Math.floor(x.actTypePointsFinished / x.actTypePoints * 100)
      });
      this.setData({ actTypeScores: scores.data.data });
    }
    if (signedActsRes.statusCode === 200 && signedActsRes.data.code === 200) {
      signedActsRes.data.data.actDatas.forEach(x => {
        x.actDesc = this.data.actTypes.find(q => q.actType == x.actType)?.actDesc;
      })
      this.setData({ signedActs: signedActsRes.data.data.actDatas });
    }
    if (handingActsRes.statusCode === 200 && handingActsRes.data.code === 200) {
      handingActsRes.data.data.actDatas.forEach(x => {
        x.actDesc = this.data.actTypes.find(q => q.actType == x.actType)?.actDesc;
      })
      this.setData({ handingActs: handingActsRes.data.data.actDatas });
    }
    if (finishedActsRes.statusCode === 200 && finishedActsRes.data.code === 200) {
      finishedActsRes.data.data.actDatas.forEach(x => {
        x.actDesc = this.data.actTypes.find(q => q.actType == x.actType)?.actDesc;
      })
      this.setData({ finishedActs: finishedActsRes.data.data.actDatas });
    }
  },
  onShareAppMessage(){
    return getApp().globalData.shareApp;
  }
})